﻿using System.Text;

namespace Shared
{
    /// <summary>
    /// 文件分片上传输入模型
    /// </summary>
    public class FileChunkUploadInput
    {
        /// <summary>
        /// 上传任务代码
        /// </summary>
        public string? UploadTaskCode { get; set; }

        /// <summary>
        /// 上传请求类型
        /// </summary>
        public string UploadType { get; set; } = null!;

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; } = null!;

        /// <summary>
        /// 文件大小
        /// </summary>
        public long? FileSize { get; set; }

        /// <summary>
        /// 支持的Hash算法，优选算法请靠前
        /// </summary>
        public List<string>? AllowedHashAlgorithm { get; set; }

        /// <summary>
        /// 使用的Hash算法
        /// </summary>
        public string? HashAlgorithm { get; set; }

        /// <summary>
        /// Hash值
        /// </summary>
        public string? HashValue { get; set; }

        /// <summary>
        /// 文件分片数量
        /// </summary>
        public int FileChunkCount { get; set; }

        /// <summary>
        /// 文件片段大小
        /// </summary>
        public int? FileChunkSize { get; set; }

        /// <summary>
        /// 文件片段偏移量（相对于整个文件）
        /// </summary>
        public long? FileChunkOffset { get; set; }

        /// <summary>
        /// 文件片段索引
        /// </summary>
        public int? FileChunkIndex { get; set; }

        /// <summary>
        /// 取消上传的原因
        /// </summary>
        public string? CancelReason { get; set; }
    }

    /// <summary>
    /// 文件分片上传开始结果
    /// </summary>
    public class FileChunkUploadStartReault
    {
        /// <summary>
        /// 上传任务代码
        /// </summary>
        public string UploadTaskCode { get; set; } = null!;

        /// <summary>
        /// 选中的Hash算法
        /// </summary>
        public string SelectedHashAlgorithm { get; set; } = null!;
    }

    /// <summary>
    /// Hash助手
    /// </summary>
    public static class HashHelper
    {
        /// <summary>
        /// 把Hash的字节数组转换为16进制字符串表示
        /// </summary>
        /// <param name="bytes">原始Hash值</param>
        /// <returns>Hash值的16进制文本表示（大写）</returns>
        public static string ToHexString(this byte[] bytes)
        {
            StringBuilder sb = new(bytes.Length * 2);
            foreach (var @byte in bytes)
            {
                sb.Append(@byte.ToString("X2"));
            }
            return sb.ToString();
        }
    }
}